package com.app.twth.utils;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


@SuppressLint("SimpleDateFormat")
public final class DateUtil {

    public static int getRangeTime() {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String date = format.format(new Date(System.currentTimeMillis()));
        date = date.replace(":", "");
        return Integer.parseInt(date);
    }

    public static String getYMDHMS() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss");
        return format.format(new Date(System.currentTimeMillis()));
    }

    public static String getYMDHMS2() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date(System.currentTimeMillis()));
    }

    public static String getYMD() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(new Date(System.currentTimeMillis()));
    }

    public static String getDateTime2(int n) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, n);
        date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(date);
    }

    /**
     * 获取第n天的日期
     *
     * @param n
     * @return
     */
    public static String getYMDate(int n) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, n);
        date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String getFormatDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(System.currentTimeMillis());
    }

    public static String getFormatTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(System.currentTimeMillis());
    }

    public static Date StringToDate(String time) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = ft.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static long StringToTime(String time) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = ft.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (date != null) {
            return date.getTime();
        }
        return 0;
    }

    public static String LongToTime(Long timeStamp) {
        String dateStr = null;
        try {

            // 转化为指定格式的字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateStr = sdf.format(new Date(timeStamp));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    public static int LongToYMD(Long timeStamp) {
        int dateYMD = 0;
        try {

            // 转化为指定格式的字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String dateStr = sdf.format(new Date(timeStamp));
            dateYMD = dateStr == null ? 0 : Integer.parseInt(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dateYMD;
    }

    /**
     * utc时间转成local时间
     *
     * @param utcTime
     * @return
     */
    public static String utcToLocal(String utcTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date utcDate = null;
        Date locatlDate = null;

        try {
            utcDate = sdf.parse(utcTime);
            sdf.setTimeZone(TimeZone.getDefault());
            String localTime = sdf.format(utcDate.getTime());
            locatlDate = sdf.parse(localTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (locatlDate == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(locatlDate);
    }

    /**
     * utc时间转成local时间
     *
     * @param utcTime
     * @return
     */
    public static String utcToUtcHint(String utcTime) {
        if (Utils.isEmpty(utcTime)) {
            return "";
        }
        utcTime = utcTime.replace("T", " ");
        utcTime = utcTime.replace("Z", " ");
        return utcTime + "(utc)";
    }

}
